package cn.fl.atc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.atc.service.IAtcBscInfoLService;
import cn.fl.atc.service.IAtcBscInfoService;
import cn.fl.atc.service.IAtcRlPsnService;
import cn.fl.atc.vo.AtcBscInfoLVO;
import cn.fl.atc.vo.AtcBscInfoVO;
import cn.fl.atc.vo.AtcRlPsnVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.ISplRlPsnService;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.vo.SysUlFilesRecVO;
import cn.fl.upload.constant.UploadConstant;
import fl.spl.facade.atc.condition.AtcBscInfoLQueryCondition;
import fl.spl.facade.atc.condition.AtcBscInfoQueryCondition;
import fl.spl.facade.atc.condition.AtcRlPsnQueryCondition;
import fl.spl.facade.atc.constant.AtcBscInfoConstant;
import fl.spl.facade.spl.constant.SplInfoConstant;
import net.sf.json.JSONArray;

/**
 * @Description: 汽贸城基础信息表 控制层
 */
@Controller
@RequestMapping("/atc/atcBscInfo")
public class AtcBscInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AtcBscInfoController.class);

	/**
	 * AtcBscInfo 服务层BEAN
	 */
	@Autowired
	private IAtcBscInfoService atcBscInfoService;
	@Autowired
	private IAtcBscInfoLService atcBscInfoLService;
	
	@Autowired
	private IAtcRlPsnService atcRlPsnService;

	@Autowired
	private ISplRlPsnService splRlPsnService;
	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET })
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("atc/autotrade/autoTradeList");
		List<KeyValueVO> searchStatusList = new ArrayList<>();
		KeyValueVO vo1 = new KeyValueVO();
		vo1.setKey("0");
		vo1.setValue("合作中");
		KeyValueVO vo2 = new KeyValueVO();
		vo2.setKey("1");
		vo2.setValue("已过期");
		searchStatusList.add(vo1);
		searchStatusList.add(vo2);
		view.addObject("searchStatusList", searchStatusList);
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到供应商列表页面
	 */
	@RequestMapping(value = "/toAtcBscInfoDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toAtcBscInfoDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("atc/autotrade/autoTradeDetail");
		String id = request.getParameter("atcId");
		BaseLogUtils.info(logger, "toAtcSplDetailPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		view.addObject("id", id);
		return view;
	}
	/**
	 * 到供应商列表页面
	 */
	@RequestMapping(value = "/toAtcSplDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toAtcSplDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("atc/autotrade/autoTradeSplList");
		String id = request.getParameter("atcId");
		BaseLogUtils.info(logger, "toAtcSplDetailPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		view.addObject("id", id);
		return view;
	}
	
	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("atc/autotrade/autoTradeAdd");
		view.addObject("id", id);
		view.addObject("loginUsrNm", CurrentThreadContext.getCurrentUserName());
		view.addObject("sysDate", DateUtils.formatDateToString(new Date(), DateUtils.YYYY_MM_DDHHMMSS));
		// 证件类型
		List<KeyValueVO> certTypCdList = this.splRlPsnService.searchCertTypCdList();
		view.addObject("certTypCdListJSON", JSON.toJSON(certTypCdList));
		//联系人类型
		List<KeyValueVO> rlTypCDList = this.splRlPsnService.searchRlTypCdList();
		view.addObject("rlTypCDListJSON", JSON.toJSON(rlTypCDList));
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String atNm = request.getParameter("atNm");
		String status = request.getParameter("status");
		String endDt = request.getParameter("endDt");
		String beginDt = request.getParameter("beginDt");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		AtcBscInfoQueryCondition qc = new AtcBscInfoQueryCondition();
		if (StringUtils.isNotEmpty(atNm)) {
			qc.setAtNm(atNm);
		}
		if (StringUtils.isNotEmpty(status)) {
			qc.setStatus(Integer.valueOf(status));
		}
		if (StringUtils.isNotEmpty(endDt)) {
			qc.setEndDt(DateUtils.formatStringToDate(endDt, DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(beginDt)) {
			qc.setBeginDt(DateUtils.formatStringToDate(beginDt, DateUtils.YYYY_MM_DD));
		}
		page.setCondition(qc);

		Pager pager = this.atcBscInfoService.searchListPage(page);
		List<AtcBscInfoVO> list = (List<AtcBscInfoVO>) pager.getResultList();
		ListPageVO<AtcBscInfoVO> listPage = new ListPageVO<AtcBscInfoVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page),
				CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	/**
	 * 初始化历史信息列表
	 */
	@RequestMapping(value = "/getAtcBscInfoLList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getAtcBscInfoLList(HttpServletRequest request, HttpServletResponse response) {
		String atcId = request.getParameter("atcId");
		Pager page = new Pager();
		
		AtcBscInfoLQueryCondition qc = new AtcBscInfoLQueryCondition();
		if (StringUtils.isNotEmpty(atcId)) {
			qc.setAtcId(Long.valueOf(atcId));
		}
		List<SysUlFilesRecVO> list = this.atcBscInfoLService.searchList(qc);
		ListPageVO<SysUlFilesRecVO> listPage = new ListPageVO<SysUlFilesRecVO>(list, list.size());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page),
				CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = { RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.atcBscInfoService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id=" + id + "]" + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id=" + id + "]" + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id=" + id + "]",
					CurrentThreadContext.getCurrentUserName());
			return result;
		}

		try {
			AtcBscInfoVO vo = this.atcBscInfoService.selectById(Long.valueOf(id));
			if (null == vo) {
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id=" + id + "]",
						CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id=" + id + "]" + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id=" + id + "]" + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		AtcBscInfoVO entity = null;
		try {
			if (StringUtils.isNotEmpty(id)) {
				// 修改
				entity = this.atcBscInfoService.selectById(Long.valueOf(id));
				// 校验合同日期是否修改，如果修改则保存历史信息
				String endDt = request.getParameter("endDt");
				Date date = DateUtils.formatStringToDate(endDt, DateUtils.YYYY_MM_DD);
				if(date.compareTo(entity.getEndDt()) != 0){
					//封装日志信息
					AtcBscInfoLVO logVO = new AtcBscInfoLVO();
					logVO.setAtcId(entity.getId());
					logVO.setBeginDt(entity.getBeginDt());
					logVO.setEndDt(entity.getEndDt());
					logVO.setCrtTm(new Date());
					logVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					entity.setLogVO(logVO);
				}
				if(!date.before(entity.getEndDt()) && entity.getStatus() == AtcBscInfoConstant.IS_STATUS_1){
					entity.setStatus(AtcBscInfoConstant.IS_STATUS_0);
				}
				// 封装基本参数
				getRequestAtcBscInfoVO(request, entity);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_SUCC);
			} else {
				// 新增
				// 封装基本参数
				entity = new AtcBscInfoVO();
				getRequestAtcBscInfoVO(request, entity);
				// 新增服务
			}
			// 封装资料信息
			String fileParams = request.getParameter("fileParams");// 附件上传信息
			JSONArray arry = JSONArray.fromObject(fileParams);
			if(arry.size() > 0){
				List<SysUlFilesRecVO> fileList = getFileList(request);
				entity.setFilesVO(fileList);
			}else{
				if(StringUtils.isEmpty(entity.getId())){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "上传资料不能为空.");
				}
			}
			
			//联系人姓名电话不能为空
			String socialRlGridJSON = request.getParameter("socialRlGridJSON");
			List<AtcRlPsnVO> rlPsnList = new ArrayList<>();
			if(StringUtils.isNotEmpty(socialRlGridJSON)){
				rlPsnList = JSON.parseArray(socialRlGridJSON, AtcRlPsnVO.class);
			}
			if(CollectionUtils.isEmpty(rlPsnList)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请维护主要人员信息.");
			}else{
				Boolean flag = false;
				for (AtcRlPsnVO rlVO : rlPsnList) {
					//验证输入项是否为空
					if(StringUtils.isEmpty(rlVO.getRlNm())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "联系人姓名不能为空.");
					}else if(StringUtils.isEmpty(rlVO.getMp())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "联系人电话不能为空.");
					}else if(StringUtils.isEmpty(rlVO.getCertTypCd())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "联系人证件类型不能为空.");
					}else if(StringUtils.isEmpty(rlVO.getCertNo())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "联系人证件号码不能为空.");
					}else if(StringUtils.isEmpty(rlVO.getRlTypCd())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "联系人类型不能为空.");
					}
					if(SplInfoConstant.SPL_RL_TYP_CD_ACTUAL_3.equals(rlVO.getRlTypCd())){
						flag = true;
					}
					rlVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					
					//验证身份证号
					String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
					if(!Pattern.matches(REGEX_ID_CARD, rlVO.getCertNo())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "身份证号格式不正确.");
					}
					//验证手机号
					String MOBLIE = "^1[3|4|5|7|8][0-9]\\d{4,8}$";
					if(!Pattern.matches(MOBLIE, rlVO.getMp())){
						throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "手机号格式不正确.");
					}
				}
				if(!flag){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请维护主要联系人.");
				}
				entity.setRlVO(rlPsnList);
			}
			Long atcId = this.atcBscInfoService.add(entity);
			
			result.setData(atcId);
			BaseLogUtils.info(logger, "add", "新增数据.[id=" + atcId + "] DATA:" + JSONObject.toJSONString(entity),
					CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity), ex);
		}

		return result;
	}

	/**
	 * 初始化人员关系列表列表
	 */
	@RequestMapping(value = "/initAtcRlPsnList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initAtcRlPsnList(HttpServletRequest request, HttpServletResponse response) {
		String atcId = request.getParameter("atcId");
		ListPageVO<AtcRlPsnVO> listPage = null;
		if (StringUtils.isEmpty(atcId)) {
			listPage = new ListPageVO<AtcRlPsnVO>(this.setAtcRlPsnList(null), 0);
			return listPage;
		} else {
			AtcRlPsnQueryCondition qc = new AtcRlPsnQueryCondition();
			qc.setAtcId(Long.valueOf(atcId));
			qc.setIsDel(AtcBscInfoConstant.IS_STATUS_0);
			List<AtcRlPsnVO> list = atcRlPsnService.searchList(qc);
			// 将未存在的必输联系人添加
			list = this.setAtcRlPsnList(list);
			listPage = new ListPageVO<AtcRlPsnVO>(list, list.size());
			BaseLogUtils.info(logger, "initSplRlPsnList", "初始化供应商联系人列表页面数据.DATA:" + JSONObject.toJSONString(list),
					CurrentThreadContext.getCurrentUserName());
		}
		return listPage;
	}
	/**
	 * 修改人员关系列表列表
	 */
	@RequestMapping(value = "/modifyRlPsn.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object modifyRlPsn(HttpServletRequest request, HttpServletResponse response) {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		String rlId = request.getParameter("rlId");
		String isDel = request.getParameter("isDel");
		if (StringUtils.isEmpty(rlId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME+",主键为空！");
			return result;
		} 
		AtcRlPsnVO entity = null;
		try{
			entity = atcRlPsnService.selectById(Long.valueOf(rlId));
			if(null == entity){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(BaseExceptionConstant.BASE_EXCEPTION_CODE_601_NAME);
				return result;
			}
			if(StringUtils.isNotEmpty(isDel)){
				entity.setIsDel(Integer.valueOf(isDel));
			}
			boolean modify = atcRlPsnService.modify(entity);
			BaseLogUtils.info(logger, "modifyRlPsn", "修改汽贸城联系人数据.DATA:" + JSONObject.toJSONString(modify),
					CurrentThreadContext.getCurrentUserName());
		}catch(BaseException e){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.info(logger, "add", "修改数据失败.DATA:" + JSONObject.toJSONString(entity) + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		}catch(Exception e){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(e.getMessage());
			BaseLogUtils.error(logger, "add", "修改数据失败.DATA:" + JSONObject.toJSONString(entity) ,
					CurrentThreadContext.getCurrentUserName(),e);
		}
		return result;
	}
	private List<AtcRlPsnVO> setAtcRlPsnList(List<AtcRlPsnVO> list) {
		// 查询出所有必输联系人信息
		//联系人类型
		List<KeyValueVO> rlTypCDList = this.splRlPsnService.searchRlTypCdList();
		Map<String, AtcRlPsnVO> map = new HashMap<>();
		if (CollectionUtils.isNotEmpty(rlTypCDList)) {
			for (KeyValueVO keyValueVO : rlTypCDList) {
				if(SplInfoConstant.SPL_RL_TYP_CD_ACTUAL_3.equals(keyValueVO.getKey())){
					AtcRlPsnVO vo = new AtcRlPsnVO();
					vo.setRlTypCd(keyValueVO.getKey());
					vo.setRlTypCdNm(keyValueVO.getValue());
					vo.setCertTypCd(AtcBscInfoConstant.MR_CERT_TYP_CD);
					vo.setCertTypCdNm(AtcBscInfoConstant.MR_CERT_TYP_CD_NM);
					map.put(keyValueVO.getKey(), vo);
				}
			}
		}
		// 判断修改的联系人信息中是否存在必输的联系人
		if (CollectionUtils.isNotEmpty(list)) {
			// 循环将已经存在的vo信息
			for (AtcRlPsnVO vo : list) {
				// 如果存在必输联系人，将必输Map中联系人去除
				if (map.containsKey(vo.getRlTypCd())) {
					map.remove(vo.getRlTypCd());
				}
			}
		} else {// 新增时或修改时无联系人信息，创建联系人
			list = new ArrayList<>();
		}
		// 将去除重复后的必输联系人添加到联系人列表中
		if (CollectionUtils.isMapNotEmpty(map)) {
			for (Map.Entry<String, AtcRlPsnVO> entry : map.entrySet()) {
				list.add(entry.getValue());
			}
		}
		return list;
	}

	/**
	 * 封装基本信息
	 * 
	 * @param request
	 * @return
	 */
	private void getRequestAtcBscInfoVO(HttpServletRequest request, AtcBscInfoVO entity) {
		
		
		String id = request.getParameter("id");
		// 修改
		if (StringUtils.isNotEmpty(id)) {
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		}else{
			// 新增
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setStatus(AtcBscInfoConstant.IS_STATUS_0);
		}
		String isDel = request.getParameter("isDel");
		if (StringUtils.isNotEmpty(isDel)) {
			entity.setIsDel(Integer.valueOf(isDel));
		} else {
			entity.setIsDel(AtcBscInfoConstant.IS_STATUS_0);
		}
		String atAddr = request.getParameter("atAddr");
		if (StringUtils.isNotEmpty(atAddr)) {
			entity.setAtAddr(atAddr);
		}else{
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请填写汽贸城地址信息！");
		}
		String atNm = request.getParameter("atNm");
		if (StringUtils.isNotEmpty(atNm)) {
			entity.setAtNm(atNm);
		}
		String rgnCyCd = request.getParameter("rgnCyCd");
		if (StringUtils.isNotEmpty(rgnCyCd)) {
			entity.setRgnCyCd(rgnCyCd);
		}
		String rgnPrCd = request.getParameter("rgnPrCd");
		if (StringUtils.isNotEmpty(rgnPrCd)) {
			entity.setRgnPrCd(rgnPrCd);
		}
		String beginDt = request.getParameter("beginDt");
		if (StringUtils.isNotEmpty(beginDt)) {
			entity.setBeginDt(DateUtils.formatStringToDate(beginDt, DateUtils.YYYY_MM_DD));
		}else{
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请选择合同开始时间！");
		}
		String endDt = request.getParameter("endDt");
		if (StringUtils.isNotEmpty(endDt)) {
			entity.setEndDt(DateUtils.formatStringToDate(endDt, DateUtils.YYYY_MM_DD));
		}else{
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "请选择合同结束时间！");
		}
	}

	/**
	 * 封装资料信息
	 * 
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private List<SysUlFilesRecVO> getFileList(HttpServletRequest request) throws BaseException {
		String fileParams = request.getParameter("fileParams");// 附件上传信息
		List<SysUlFilesRecVO> sysUlFilesRecVOList = new ArrayList<SysUlFilesRecVO>();
		// 解析附件上传信息
		if (StringUtils.isNotEmpty(fileParams)) {
			List<Map<String, String>> fileParamsList = new ArrayList<Map<String, String>>();
			JSONArray arry = JSONArray.fromObject(fileParams);
			if (!(arry.size() > 0)) {
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "没有要保存的资料信息");
			}
			for (int i = 0; i < arry.size(); i++) {
				net.sf.json.JSONObject jsonObject = arry.getJSONObject(i);
				Iterator<?> it = jsonObject.keys();
				HashMap<String, String> hMap = new HashMap<String, String>();
				while (it.hasNext()) {
					String key = String.valueOf(it.next());
					String value = (String) jsonObject.get(key);
					hMap.put(key, value);
				}
				fileParamsList.add(hMap);
			}
			for (Map<String, String> map : fileParamsList) {
				SysUlFilesRecVO sysUlFilesRecVO = new SysUlFilesRecVO();
				sysUlFilesRecVO.setFileNm(map.get("fileNm"));
				sysUlFilesRecVO.setFilePath(map.get("filePath"));
				sysUlFilesRecVO.setAbsLocPath(
						PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_ROOT_PATH) + map.get("filePath"));
				sysUlFilesRecVO.setCrtTm(new Date());
				sysUlFilesRecVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
//				sysUlFilesRecVO.setPkId(pkId);
				// TODO 这里应该先读配置 按配置保存 这里先写死后期再改---------------Start
				sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
				sysUlFilesRecVO.setOrgCd("0000");
				sysUlFilesRecVO.setCfgCd(AtcBscInfoConstant.ATC_FILE_UPLOAD_CFG_CD);
				sysUlFilesRecVO.setCfgDtlCd(AtcBscInfoConstant.ATC_FILE_UPLOAD_CFG_DTL_CD);
				// TODO---------------------------------------------------------End
				// 默认验证状态:未验证
				sysUlFilesRecVO.setAudStsCd("0");
				sysUlFilesRecVO.setTabNm(AtcBscInfoConstant.UPLOAD_TAB_NM);
				sysUlFilesRecVO.setIsDel(BaseConstant.IS_YESNO_NO);
				sysUlFilesRecVOList.add(sysUlFilesRecVO);
			}
		}
		return sysUlFilesRecVOList;
	}

	/**
	 * 根据字典中代码组值获取组员集合
	 * 
	 * @param grpCd
	 *            代码组值
	 * @return 集合
	 */
	public List<KeyValueVO> getKvList(String grpCd) {
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService
					.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if (CollectionUtils.isNotEmpty(sysDictionaryDTOList)) {
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组" + grpCd + "失败.ERROR:" + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组" + grpCd + "失败,系统错误.ERROR:" + e.getMessage(),
					CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}
}
